const express = require('express');
const router = express.Router();
const db = require('../db');

// 获取节假日数据
router.get('/', (req, res) => {
  const { start, end } = req.query;

  let sql = 'SELECT * FROM holidays';
  const params = [];
  const conditions = [];

  if (start && end) {
    conditions.push('date BETWEEN ? AND ?');
    params.push(start, end);
  } else if (start) {
    conditions.push('date >= ?');
    params.push(start);
  } else if (end) {
    conditions.push('date <= ?');
    params.push(end);
  }

  if (conditions.length > 0) {
    sql += ' WHERE ' + conditions.join(' AND ');
  }

  sql += ' ORDER BY date ASC';

  db.all(sql, params, (err, rows) => {
    if (err) {
      res.status(500).json({ error: err.message });
      return;
    }
    res.json(rows);
  });
});

module.exports = router;